package com.huikeportal.service.impl;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.huikeportal.controller.common.MessageManager;
import com.huikeportal.dao.DataTransInfoMapper;
import com.huikeportal.dao.PosAllInfoMapper;
import com.huikeportal.dao.PosDataManagerMapper;
import com.huikeportal.model.MessageCommon;
import com.huikeportal.model.PosAllInfo;
import com.huikeportal.model.PosApplication;
import com.huikeportal.model.PosDataManagerInfo;
import com.huikeportal.model.SiteAllInfo;
import com.huikeportal.model.SiteType;
import com.huikeportal.model.UserInfo;
import com.huikeportal.model.WordBook;
import com.huikeportal.model.common.PageDataVo;
import com.huikeportal.service.PosService;


@Service("posService")
public class PosServiceImpl implements PosService {

	@Autowired
	private PosDataManagerMapper posDataManagerMapper;
	@Autowired
	private PosAllInfoMapper posAllInfoMapper;
	@Autowired
	private DataTransInfoMapper dataTransInfoMapper;
	
	/**
	 * POS数据管理分页
	 */
	public PageDataVo getPosSiteList(PageDataVo pageDataVo) throws Exception {
		int maxTotal = posDataManagerMapper.queryPosSiteTotal(pageDataVo);
		List<PosDataManagerInfo> posSiteList = posDataManagerMapper
				.queryPosSiteList(pageDataVo);
		pageDataVo.setMaxRow(maxTotal);
		pageDataVo.setMaxPage((maxTotal + pageDataVo.getPageSize() - 1)
				/ pageDataVo.getPageSize());
		pageDataVo.setPageResult(posSiteList);
		return pageDataVo;
	}
	
	public PageDataVo getPosDetail(PageDataVo pageDataVo) throws Exception {
		int maxTotal = posDataManagerMapper.getPosDetailTotal(pageDataVo);
		List<PosDataManagerInfo> posSiteList = posDataManagerMapper
				.getPosDetailList(pageDataVo);
		pageDataVo.setMaxRow(maxTotal);
		pageDataVo.setMaxPage((maxTotal + pageDataVo.getPageSize() - 1)
				/ pageDataVo.getPageSize());
		pageDataVo.setPageResult(posSiteList);
		return pageDataVo;
	}

	public PosDataManagerInfo getPosDataById(
			PosDataManagerInfo posDataManangerInfo) throws Exception {
		return null;
	}

	public List<SiteAllInfo> getSitesByUser(UserInfo userInfo) throws Exception {
		return posDataManagerMapper.getSitesByUser(userInfo);
	}
	
	
	public int getLowPermission(List<SiteAllInfo> siteList){
		int permission = 1000;
		for(int i=0,len = siteList.size();i<len;i++){
			if(siteList.get(i).getType()!= null){
				int currentPermission = siteList.get(i).getType();
				permission = permission < currentPermission ? permission : currentPermission;
			}
		}
		
		return permission;
	}
	
	public List<SiteAllInfo> getAllGcList(List<SiteAllInfo> siteList) throws Exception {
		List<SiteAllInfo> allGcList = new ArrayList<SiteAllInfo>();
		for(int i=0,len = siteList.size(); i<len; i++){
			SiteAllInfo temp = siteList.get(i);
			int type = temp.getType();
			if(type == 0){
				List<SiteAllInfo> jtList = posDataManagerMapper.getAllJtSites(temp);
				for(int j=0,length=jtList.size();j<length;j++){
					SiteAllInfo current = jtList.get(j);
					List<SiteAllInfo> gcList = posDataManagerMapper.getAllGcSitesByJt(current);
					allGcList.addAll(gcList);
				}
			}else if(type == 100){
				List<SiteAllInfo> gcList = posDataManagerMapper.getAllGcSitesByJt(temp);
				allGcList.addAll(gcList);
			}else if(type == 200){
				List<SiteAllInfo> gcList = posDataManagerMapper.getAllGcSitesByDy(temp);
				allGcList.addAll(gcList);
			}else if(type == 300){
				// 当用户拥有店铺权限和广场权限时，如果该店铺不属于广场则添加店铺，否则不添加
				int length = allGcList.size();
				boolean flag = false;
				for(int j=0;j<length;j++){
					String siteKey = allGcList.get(j).getSiteid();
					if(siteKey.equals(temp.getSiteid())){
						flag = true;
					}
				}
				if(!flag){
					allGcList.add(temp);
				}
			}
		}
		return allGcList;
	}
	
	// 获取广场下的所有场所
	public List<SiteAllInfo> getAllSitesBySquare(List<SiteAllInfo> squareList) throws Exception{
		List<SiteAllInfo> allSiteList = new ArrayList<SiteAllInfo>();
		for(int i=0,len=squareList.size();i<len;i++){
			SiteAllInfo temp = squareList.get(i);
			List<SiteAllInfo> tempList = posDataManagerMapper.getAllSitesByGc(temp);
			allSiteList.addAll(tempList);
		}
		return allSiteList;
	}
	public List<SiteType> getSiteTypes(String para) throws Exception{
		List<WordBook> wordBooks = posDataManagerMapper.getSiteTypes(para);
		List<SiteType> siteTypes = new ArrayList<SiteType>();
		for(int i=0,len=wordBooks.size();i<len;i++){
			SiteType siteType = new SiteType();
			WordBook book = wordBooks.get(i);
			if(book != null){
				siteType.setSiteTypeName(book.getDicValue());
				siteType.setSiteTypeId(book.getDicCode());
			}
			siteTypes.add(siteType);
		}
		return siteTypes;
	}
	
	public List<SiteAllInfo> getAllSiteList(List<SiteAllInfo> siteList) throws Exception{
		List<SiteAllInfo> allSiteList = new ArrayList<SiteAllInfo>();
		for(int i=0,len=siteList.size();i<len;i++){
			SiteAllInfo temp = siteList.get(i);
			List<SiteAllInfo> tempList = posDataManagerMapper.getAllSitesByGc(temp);
			allSiteList.addAll(tempList);
		}
		
		allSiteList.addAll(siteList);
		return allSiteList;
	}
	
	public String getSitePermitAreaStr(List<SiteAllInfo> sitePermitList){
		StringBuffer buffer = new StringBuffer();
		for(int i=0,len=sitePermitList.size();i<len;i++){
			SiteAllInfo temp = sitePermitList.get(i);
			buffer.append(temp.getSiteid());
			buffer.append(",");
		}
		return buffer.toString();
	}
	

	public Map addPosData(PosDataManagerInfo posDataManagerInfo)
			throws Exception {
		Map<String,Object> map = new HashMap<String,Object>();
		Date insertTime = new Date();
		PosAllInfo posAllInfo = new PosAllInfo();
		posAllInfo.setCreatetime(insertTime);
		posAllInfo.setModifiedtime(insertTime);
		posAllInfo.setTransactiontime(posDataManagerInfo.getTransactionTime());
		posAllInfo.setSalesamount(posDataManagerInfo.getSalesAmount());
		posAllInfo.setSitekey(posDataManagerInfo.getSiteKey());
		posAllInfo.setDetails(posDataManagerInfo.getDetails());
		posAllInfo.setInputType(posDataManagerInfo.getInputType());
		posAllInfo.setTransactionnumber(posDataManagerInfo.getTransactionNumber());
		posAllInfo.setSource(Integer.parseInt(MessageManager
				.getMsg("insertSource")));
		posAllInfo.setFlag("0");
		posAllInfo.setInputType("1");
		posAllInfoMapper.insertSelective(posAllInfo);
		
		map.put("posDataId", posAllInfo.getId());
		// 插入日志表
		return map;
	}
	
	
	public Map addPosDataOfImport(PosDataManagerInfo posDataManagerInfo)
	throws Exception {
		Map<String,Object> map = new HashMap<String,Object>();
		Date insertTime = new Date();
		PosAllInfo posAllInfo = new PosAllInfo();
		posAllInfo.setCreatetime(insertTime);
		posAllInfo.setModifiedtime(insertTime);
		posAllInfo.setTransactiontime(posDataManagerInfo.getTransactionTime());
		posAllInfo.setSalesamount(posDataManagerInfo.getSalesAmount());
		posAllInfo.setSitekey(posDataManagerInfo.getSiteKey());
		posAllInfo.setDetails(posDataManagerInfo.getDetails());
		posAllInfo.setInputType(posDataManagerInfo.getInputType());
		posAllInfo.setTransactionnumber(posDataManagerInfo.getTransactionNumber());
		posAllInfo.setSource(Integer.parseInt(MessageManager
				.getMsg("insertSource")));
		posAllInfo.setFlag("0");
		posAllInfo.setInputType("2");
		posAllInfoMapper.insertSelective(posAllInfo);
		
		map.put("posDataId", posAllInfo.getId());
		
		// 插入日志表
		return map;
		}
	
	public MessageCommon addPosDataDetail(PosDataManagerInfo posDataManagerInfo)
	throws Exception {
		MessageCommon message = new MessageCommon();
		
		PosAllInfo posAllInfo = new PosAllInfo();
		posAllInfo.setCreatetime(new Date());
		posAllInfo.setModifiedtime(new Date());
		posAllInfo.setTransactiontime(posDataManagerInfo.getTransactionTime());
		posAllInfo.setSalesamount(posDataManagerInfo.getSalesAmount());
		posAllInfo.setSitekey(posDataManagerInfo.getSiteKey());
		posAllInfo.setDetails(posDataManagerInfo.getDetails());
		posAllInfo.setInputType("2");
		posAllInfo.setTransactionnumber(posDataManagerInfo.getTransactionNumber());
		posAllInfo.setSource(Integer.parseInt(MessageManager
				.getMsg("insertSource")));
		posAllInfo.setFlag("0");
		posAllInfo.setInputType("1");
		posAllInfoMapper.insertSelective(posAllInfo);
		
		message.setFlag(true);
		message.setMessage("新增POS数据成功！");
		// 插入日志表
		return message;
    }
	
	
	public MessageCommon updatePosData(PosDataManagerInfo posDataManagerInfo)
	throws Exception {
		MessageCommon message = new MessageCommon();
		
		PosAllInfo posAllInfo = new PosAllInfo();
		posAllInfo.setId(posDataManagerInfo.getId());
		posAllInfo.setModifiedtime(new Date());
		posAllInfo.setTransactiontime(posDataManagerInfo.getTransactionTime());
		posAllInfo.setSalesamount(posDataManagerInfo.getSalesAmount());
		posAllInfo.setSitekey(posDataManagerInfo.getSiteKey());
		posAllInfo.setDetails(posDataManagerInfo.getDetails());
		posAllInfo.setTransactionnumber(posDataManagerInfo.getTransactionNumber());
		posAllInfo.setSource(Integer.parseInt(MessageManager
				.getMsg("insertSource")));
		posAllInfo.setFlag("0");
		posAllInfo.setInputType("2");
		posDataManagerMapper.updateById(posAllInfo);
		
		message.setFlag(true);
		message.setMessage("编辑POS数据成功！");
		// 插入日志表
		return message;
		
	}
	
	public MessageCommon updateDataOfImport(PosAllInfo info)
	throws Exception {
		MessageCommon message = new MessageCommon();
		
		PosAllInfo posAllInfo = new PosAllInfo();
		posAllInfo.setId(info.getId());
		posAllInfo.setModifiedtime(new Date());
		posAllInfo.setTransactiontime(info.getTransactiontime());
		posAllInfo.setSalesamount(info.getSalesamount());
		posAllInfo.setSitekey(info.getSitekey());
		posAllInfo.setDetails(info.getDetails());
		posAllInfo.setTransactionnumber(info.getTransactionnumber());
		posAllInfo.setSource(Integer.parseInt(MessageManager
				.getMsg("insertSource")));
		posAllInfo.setFlag("0");
		posAllInfo.setInputType("2");
		posDataManagerMapper.updateById(posAllInfo);
		
		message.setFlag(true);
		message.setMessage("编辑POS数据成功！");
		// 插入日志表
		return message;
		
	}
	
	public MessageCommon updatePosDataDetail(PosDataManagerInfo posDataManagerInfo)
	throws Exception {
		MessageCommon message = new MessageCommon();
		
		PosAllInfo posAllInfo = new PosAllInfo();
		posAllInfo.setId(posDataManagerInfo.getId());
		posAllInfo.setModifiedtime(new Date());
		posAllInfo.setTransactiontime(posDataManagerInfo.getTransactionTime());
		posAllInfo.setSalesamount(posDataManagerInfo.getSalesAmount());
		posAllInfo.setSitekey(posDataManagerInfo.getSiteKey());
		posAllInfo.setDetails(posDataManagerInfo.getDetails());
		posAllInfo.setTransactionnumber(posDataManagerInfo.getTransactionNumber());
		posAllInfo.setSource(Integer.parseInt(MessageManager
				.getMsg("insertSource")));
		posAllInfo.setFlag("0");
		posAllInfo.setInputType("2");
		posDataManagerMapper.updateById(posAllInfo);
		
		message.setFlag(true);
		message.setMessage("编辑POS数据成功！");
		// 插入日志表
		return message;
	}
	
	
	public MessageCommon deletePosData(PosDataManagerInfo posDataManagerInfo)
			throws Exception {
		PosAllInfo posAllInfo = new PosAllInfo();
		posAllInfo.setSitekey(posDataManagerInfo.getSiteKey());
		posAllInfo.setTransactiontime(posDataManagerInfo.getTransactionTime());
		posAllInfo.setFlag("3");
		posDataManagerMapper.deleteInLogic(posAllInfo);
		
		MessageCommon message = new MessageCommon();
		message.setFlag(true);
		message.setMessage("删除POS数据成功！");
		// 插入日志表
		return message;
	}
	
	public MessageCommon deletePosDataDetail(PosDataManagerInfo posDataManagerInfo)
	throws Exception {
			PosAllInfo posAllInfo = new PosAllInfo();
			posAllInfo.setFlag("3");
			posAllInfo.setId(posDataManagerInfo.getId());
			posDataManagerMapper.deleteDetailInLogic(posAllInfo);
			
			MessageCommon message = new MessageCommon();
			message.setFlag(true);
			message.setMessage("删除POS数据成功！");
			// 插入日志表
			return message;
		}
	
	/**
	 * 导航栏日期
	 * 当前时间：2015-11-16 下午
	 * @return
	 * @throws Exception
	 */
	@SuppressWarnings("deprecation")
	public String getDate() throws Exception{
		Date date=new Date();
		DateFormat format1=new SimpleDateFormat("yyyy-MM-dd");
		String str=format1.format(date);
		int hh=date.getHours();
		String restr = "";
		if(hh<12){
			restr="当前时间："+str+" 上午 ";
		}
		else
		{
			restr="当前时间："+str+" 下午 ";
		}
		return restr;
	}

	public String getPosDataIdByParams(PosDataManagerInfo posDataManagerInfo) {
		PosAllInfo posAllInfo = new PosAllInfo();
		posAllInfo.setSitekey(posDataManagerInfo.getSiteKey());
		posAllInfo.setCreatetime(posDataManagerInfo.getBeginTime());
		return posDataManagerMapper.getPosDataIdByParams(posAllInfo);
	}

	public List<PosApplication> getTemplateColumns(PosApplication info) throws Exception {
		return dataTransInfoMapper.getTemplateColumns(info);
	}

	public PosAllInfo getDataBySiteKAndTransT(PosDataManagerInfo info) {
		return posDataManagerMapper.getDataBySiteKAndTransT(info);
	}

}
